<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAll, deleteApi } from '@/api/shop'

// 表格数据
const tableData = ref([])
// 加载状态
const loading = ref(false)
// 分页参数
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
// 查询参数
const queryParams = ref({
  title: ''
})

// 树形结构配置
const treeProps = ref({
  children: 'children',
  label: 'name'
})

// 获取商品列表
const getShopList = async () => {
  loading.value = true
  try {
    const res = await getAll(
      queryParams.value.title,
      currentPage.value,
      pageSize.value
    )
    tableData.value = res.data?.rows || res.data || []
    total.value = res.data?.total || 0
  } catch (error) {
    ElMessage.error('获取商品列表失败: ' + error.message)
    console.error(error)
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  currentPage.value = 1
  getShopList()
}

// 重置
const resetQuery = () => {
  queryParams.value.title = ''
  currentPage.value = 1
  getShopList()
}

// 分页改变
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  getShopList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getShopList()
}

// 删除商品
const handleDelete = async (row) => {
  try {
    const res = await deleteApi(row.id)
    if (res.code) {
      ElMessage.success('删除成功')
      getShopList()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  } catch (error) {
    ElMessage.error('删除失败: ' + error.message)
    console.error(error)
  }
}

// 页面加载时获取数据
onMounted(() => {
  getShopList()
})
</script>

<template>
  <div class="shop-management" style="width: 800px;">
    <!-- 查询表单 -->
    <el-form :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="商品名称" prop="title">

        <el-input v-model="queryParams.title" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <div class="operate-btn" style="margin: 10px 0;">
      <router-link to="/shopadd">
        <el-button type="primary">新增</el-button>
      </router-link>
    </div>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="tableData" :tree-props="treeProps" row-key="id" default-expand-all
      :border="false" style="width: 100%;">
      <el-table-column type="expand">
        <template #default="props">
          <el-table :data="props.row.skuList" style="width: 100%">
            <el-table-column label="价格">
              <template #default="scope">
                <span>{{ scope.row.realPrice }}</span>
              </template>
            </el-table-column>
            <el-table-column label="重量">
              <template #default="scope">
                <span>{{ scope.row.productWeight }}</span>
              </template>
            </el-table-column>
            <el-table-column label="体积">
              <template #default="scope">
                <span>{{ scope.row.productVolume }}</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="title" label="商品名称" />
      <el-table-column prop="content" label="商品详情" />
      <el-table-column prop="description" label="商品描述" />
      <el-table-column label="操作" width="200px" align="center">
        <template #default="scope">
          <router-link :to="`/shopadd?id=${scope.row.id}`">
            <el-button size="small" type="primary" icon="Edit">修改</el-button>
          </router-link>

          <!-- 添加删除按钮 -->
          <el-button size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)"
            style="margin-left: 12px">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="demo-pagination-block" style="margin-top: 10px; text-align: right;">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<style scoped>
.shop-management {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 40px);
}

.operate-btn {
  display: flex;
  justify-content: flex-start;
  gap: 10px;
}
</style>